<!--
 * @Author: lh
 * @Date: 2023-07-11 13:52:31
 * @LastEditors: lh
 * @LastEditTime: 2023-10-17 10:52:56
-->
<template>
  <div>
    <!-- :searchDataList="searchDataListJy" -->
    <!-- <personnel></personnel> -->
    <tabCom
      @Refresh="Refresh"
      @search-callback="searchCallback"
      :title="'发函输入'"
      :operation-api="operationApiA"
      :table-data-rule="tableDataRuleA"
      :search-data-list="searchDataListJy"
      :needPagination="true"
      :update_show_key="'id'"
      :update_show_value="[0]"
      :formDataWidth="'30%'"
      :addButtonList="addButtonList"
      @buttonMoudle="buttonMoudle"
      ref="tabCombox"
    >
      <template #add="{ formData }">
        <el-form-item :label="item.title" v-for="(item, index) in searchDataListJyEdit" :key="index">
          <el-input v-if="!item.type" v-model="formData[item.name]" placeholder="请输入" />
          <personnel v-model="formData[item.name]" v-else-if="item.type == 'user'"></personnel>
          <personnelMore v-model="formData[item.name]" v-else-if="item.type == 'usermore'"></personnelMore>
          <el-input
            v-else-if="item.type == 'number'"
            type="number"
            v-model="formData[item.name]"
            placeholder="请输入"
          />
          <!-- value-format="YYYY/MM/DD" -->
          <el-date-picker
            v-else-if="item.type == 'timeday'"
            v-model="formData[item.name]"
            type="date"
            format="YYYY/MM/DD"
            placeholder="请选择"
          />
          <el-date-picker
            v-else-if="item.type == 'datetime'"
            v-model="formData[item.name]"
            type="datetime"
            format="YYYY/MM/DD HH:mm:ss"
            placeholder="请选择"
          />
          <upfilebox
            v-else-if="item.type == 'file'"
            fileType="file"
            v-model="formData[item.name]"
            v-model:name="formData[item.prop]"
          ></upfilebox>
          <el-date-picker
            @change="chooseTimeChange(formData[item.name])"
            v-else-if="item.type == 'time'"
            v-model="formData[item.name]"
            type="datetimerange"
            format="YYYY/MM/DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <el-select
            v-else-if="item.type == 'select' && item!.list!.length > 0"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            allow-create
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.value || items.id"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'selectLable'"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'multipleSelect'"
            multiple
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
        </el-form-item>
      </template>
    </tabCom>
    <!-- 关联人填写反馈进度 -->
    <el-dialog v-model="dialogVisible" :title="'关联人填写反馈进度'" @close="resetForm" width="30%">
      <el-form ref="formRef" label-width="100px" label-position="left">
        <el-form-item prop="actionItemEvolve" label="行动项进展">
          <el-input v-model="actionItemEvolve" placeholder="请输入行动项进展" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import tabCom from "@/components/table/indexcopy.vue"
// import { gethomeStatistics } from "@/api/home/index"
import { ref, onMounted } from "vue"
import upfilebox from "@/components/table/upfile.vue"
import { useUserStoreHook } from "@/store/modules/user"
import { ElMessage, ElMessageBox } from "element-plus"
import { relevanceUserAdd, directorVerifyFinish, createUserVerifyFinish } from "@/api/meeting"

const { userId } = useUserStoreHook()
const tabCombox = ref<typeof tabCom>()

onMounted(() => {
  init()
})
// 接口请求配置
const operationApiA = ref<any>([
  { type: "add", interface: "/receivingLetters/insertOrUpdate" },
  { type: "update", interface: "/receivingLetters/insertOrUpdate" },
  { type: "search", interface: "/receivingLetters/selectAll" },
  { type: "delete", interface: "/receivingLetters/deleteByIds" },
  { type: "svg", interface: "/receivingLetters/export" }
])
//#region  添加按钮所有逻辑
// 添加按钮
const addButtonList = [
  // { name: "关联人填写反馈行动项进展", type: "infill" },
  // { name: "责任人验证会议关闭申请", type: "validation" },
  // { name: "提出人验证并关闭待办任务", type: "CloseTask" }
]
const tablerow = ref() // 当前操作表单数据
// 按钮回调
const buttonMoudle = (type: string, row: any, Face: string) => {
  tablerow.value = row
  console.log(type, row, "-*-buttonMoudle")
  switch (type) {
    case "infill": // 填写反馈行动项进展
      infillFn()
      break
    case "validation": // 验证会议关闭申请
      validationFn()
      break
    case "CloseTask": //验证并关闭待办任务
      CloseTaskFn()
      break
  }
}
let dialogVisible = ref(false) // 填写反馈行动项进展弹框
let actionItemEvolve = ref("") // 行动项进展
// 填写反馈行动项进展 方法
const infillFn = () => {
  console.log("infillFn", userId)
  // 当前还没验证
  if (tablerow.value.status == 0) {
    if (userId == tablerow.value.supervisionUid) {
      dialogVisible.value = true
    } else {
      ElMessage.error("你不是该关联人~")
    }
  } else {
    ElMessage({
      message: "已操作",
      type: "warning"
    })
  }
}
// 重置数据
const resetForm = () => {
  actionItemEvolve.value = ""
}
// 确认反馈
const handleCreate = () => {
  if (!actionItemEvolve.value) {
    ElMessage.error("行动项进展不能为空")
    return
  }
  relevanceUserAdd({
    actionItemEvolve: actionItemEvolve.value,
    id: tablerow.value.id
  }).then((res) => {
    console.log(res, "---res")

    if (res.code == 200) {
      ElMessage({ message: "提交成功", type: "success" })
      tabCombox.value.resetSearch()
      dialogVisible.value = false
      resetForm()
    }
  })
}
// 负责人验证
const validationFn = () => {
  // 当前还没验证
  if (tablerow.value.status == 1) {
    if (userId == tablerow.value.directorUserId) {
      ElMessageBox.confirm("请确认是否完善?", "责任人验证会议关闭申请", {
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      })
        .then(() => {
          directorVerifyFinish(tablerow.value.id).then((res) => {
            if (res.code == 200) {
              ElMessage({ message: "提交成功", type: "success" })
              tabCombox.value.resetSearch()
              resetForm()
            }
          })
        })
        .catch(() => {})
    } else {
      ElMessage.error("你不是该负责人~")
    }
  } else {
    if (tablerow.value.status == 0) {
      ElMessage({
        message: "还没到当前步骤",
        type: "warning"
      })
    } else {
      ElMessage({
        message: "已操作",
        type: "warning"
      })
    }
  }
}
// 提出人验证
const CloseTaskFn = () => {
  // 当前还没验证
  if (tablerow.value.status == 2) {
    if (userId == tablerow.value.createUserId) {
      ElMessageBox.confirm("请确认是否关闭?", "提出人验证并关闭待办任务", {
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      })
        .then(() => {
          createUserVerifyFinish(tablerow.value.id).then((res) => {
            if (res.code == 200) {
              ElMessage({ message: "提交成功", type: "success" })
              tabCombox.value.resetSearch()
              resetForm()
            }
          })
        })
        .catch(() => {})
    } else {
      ElMessage.error("你不是该提出人~")
    }
  } else {
    if (tablerow.value.status < 2) {
      ElMessage({
        message: "还没到当前步骤",
        type: "warning"
      })
    } else {
      ElMessage({
        message: "已操作",
        type: "warning"
      })
    }
  }
}

//#endregion

// 搜索配置
const searchDataListJy = [
  { name: "id", title: "会议id" },
  { name: "projectName", title: "项目名称" },
  { name: "receivingUnit", title: "收文单位" },
  { name: "theme", title: "主题" },
  { name: "lettersType", title: "函件类型" },
  { name: "myReceivingOrder", title: "我方发文号" },
  { name: "otherComeOrder", title: "对方来文号" },
  {
    name: "lettersState",
    title: "函件状态",
    type: "select",
    list: [
      { value: 0, label: "待分发", name: "待分发", id: 0 },
      { value: 1, label: "已分发", name: "已分发", id: 1 },
      { value: 2, label: "已作废", name: "已作废", id: 2 }
    ]
  },
  {
    name: "isReply",
    title: "是否回复",
    type: "select",
    list: [
      { value: 0, label: "否", name: "否", id: 0 },
      { value: 1, label: "是", name: "是", id: 1 }
    ]
  },
  {
    name: "examineState",
    title: "审核状态",
    type: "select",
    list: [
      { value: 0, label: "专业监理负责人审核", name: "专业监理负责人审核", id: 0 },
      { value: 1, label: "总监/副总监审核", name: "总监/副总监审核", id: 1 },
      { value: 2, label: "驳回", name: "驳回", id: 2 },
      { value: 3, label: "关闭", name: "关闭", id: 3 }
    ]
  },
  { name: "startTime", title: "开始时间", type: "datetime" },
  { name: "endTime", title: "结束时间", type: "datetime" }
]
// 添加配置
const searchDataListJyEdit = [
  { name: "projectName", title: "项目名称" },
  { name: "receivingUnit", title: "收文单位" },
  { name: "theme", title: "主题" },
  { name: "lettersType", title: "函件类型" },
  { name: "myReceivingOrder", title: "我方发文号" },
  { name: "otherComeOrder", title: "对方来文号" },
  {
    name: "lettersState",
    title: "函件状态",
    type: "select",
    list: [
      { value: 0, label: "待分发", name: "待分发", id: 0 },
      { value: 1, label: "已分发", name: "已分发", id: 1 },
      { value: 2, label: "已作废", name: "已作废", id: 2 }
    ]
  },
  { name: "annexFileUrl", title: "附件", type: "file", prop: "annexFileName" },
  {
    name: "isReply",
    title: "是否回复",
    type: "select",
    list: [
      { value: 0, label: "否", name: "否", id: 0 },
      { value: 1, label: "是", name: "是", id: 1 }
    ]
  },
  { name: "replyEndDate", title: "回复期限", type: "datetime" },
  // {
  //   name: "examineState",
  //   title: "审核状态",
  //   type: "select",
  //   list: [
  //     { value: 0, label: "专业监理负责人审核", name: "专业监理负责人审核", id: 0 },
  //     { value: 1, label: "总监/副总监审核", name: "总监/副总监审核", id: 1 },
  //     { value: 2, label: "驳回", name: "驳回", id: 2 },
  //     { value: 3, label: "关闭", name: "关闭", id: 3 }
  //   ]
  // },
  { name: "supervisionUid", title: "专业监理负责人审核", type: "user" },
  { name: "directorUid", title: "总监/副总监审核", type: "user" },
  { name: "copiedUids", title: "内部抄送人", type: "usermore" },

  { name: "remarks", title: "备注" }
]
// 表单配置
const tableDataRuleA = ref([
  { prop: "id", label: "id" },
  { prop: "projectName", label: "项目名称" },
  { prop: "receivingUnit", label: "收文单位" },
  { prop: "theme", label: "主题" },
  { prop: "lettersType", label: "函件类型" },
  { prop: "myReceivingOrder", label: "我方发文号" },
  { prop: "otherComeOrder", label: "对方来文号" },
  { prop: "annexFileName", label: "附件", type: "filename", assoc: "annexFileUrl" },
  {
    prop: "lettersState",
    label: "函件状态",
    type: "select",
    list: [
      { value: 0, label: "待分发", name: "待分发", id: 0 },
      { value: 1, label: "已分发", name: "已分发", id: 1 },
      { value: 2, label: "已作废", name: "已作废", id: 2 }
    ]
  },
  {
    prop: "isReply",
    label: "是否回复",
    type: "select",
    list: [
      { value: 0, label: "否", name: "否", id: 0 },
      { value: 1, label: "是", name: "是", id: 1 }
    ]
  },
  { prop: "replyEndDate", label: "回复期限", type: "time" },
  {
    prop: "examineState",
    label: "审核状态",
    type: "select",
    list: [
      { value: 0, label: "专业监理负责人审核", name: "专业监理负责人审核", id: 0 },
      { value: 1, label: "总监/副总监审核", name: "总监/副总监审核", id: 1 },
      { value: 2, label: "驳回", name: "驳回", id: 2 },
      { value: 3, label: "关闭", name: "关闭", id: 3 }
    ]
  },
  { prop: "supervisionName", label: "专业监理负责人审核" },
  { prop: "directorName", label: "总监/副总监审核" },
  { prop: "copiedNames", label: "内部抄送人" },
  { prop: "remarks", label: "备注" }
  // {prop: 'operate',label: '操作'},
])
// 初始化
const init = () => {}
// 表单刷新
const Refresh = () => {
  //用户数据概览
}
// 搜索回调
const searchCallback = (search: any) => {}
const loading = ref<boolean>(false)
const gailan = ref<any>({})
const gethomeStatisticsStar = (obj: any = {}) => {
  // loading.value = true
  // gethomeStatistics(obj).then((res: any) => {
  //   gailan.value = res.data
  //   loading.value = false
  // })
}

// 时间选择限定范围
const chooseTimeChange = (time: any) => {
  const starTime = JSON.parse(JSON.stringify(time[0]))
  const endTime = JSON.parse(JSON.stringify(time[1]))
  const minTime = new Date(insertStr(insertStr(starTime, 4, "-"), 7, "-")).getTime()
  const maxTime = new Date(insertStr(insertStr(endTime, 4, "-"), 7, "-")).getTime()
  const allow = 2592000000 //30天
  if (maxTime - minTime > allow) {
    ElMessage.warning("选择范围请在30天以内!")
    chaxunState.value = false
  } else {
    chaxunState.value = true
  }
}
</script>
<style scoped lang="scss">
.search-wrapper {
  margin: 0 auto;
  width: 98.5%;
  margin-top: 10px;
}
</style>
